package com.hzdp.web.ops.remote;

public class IpScope {
	public long startIp = -1;
	public long endIp = -1;
	public String input;

	public static IpScope getIpScope(String input) {
		String[] subScopes = input.split("/");
		if (subScopes.length != 2) {
			return new IpScope();
		}

		long mainIp = RemoteIpGetter.getIpNumber(subScopes[0]);
		if (mainIp < 0) {
			return new IpScope();
		}

		int scope;
		try {
			scope = Integer.parseInt(subScopes[1]);
		} catch (Exception ex) {
			return new IpScope();
		}
		if (scope <= 0 || scope > 32) {
			return new IpScope();
		}

		IpScope result = new IpScope();
		result.input = input;
		int scopeMaskOffset = 32 - scope;
		long mask = 1 << scopeMaskOffset;
		result.startIp = mainIp >> scopeMaskOffset << scopeMaskOffset;
		result.endIp = result.startIp + mask;

		return result;
	}

	public boolean isInScope(long ipNumber) {
		if (ipNumber >= startIp && ipNumber < endIp) {
			return true;
		} else {
			return false;
		}
	}
}
